<!--  线 + 柱混合图 -->
<template>
  <el-card>
    <template #header>
      <div class="title">
        营业趋势
        <el-tooltip effect="dark" content="点击试试下载" placement="bottom">
            <el-icon class="download" @click="downloadEchart"><Download /></el-icon>
        </el-tooltip>
      </div>
    </template>

    <div :id="id" style="width: 100%; height: 365px" :class="className" :style="{ height, width }"></div>
  </el-card>
</template>

<script setup lang="ts">
import * as echarts from "echarts"
import { ref, onMounted,markRaw,toRaw } from "vue";
import {Download, Printer, Sell} from '@element-plus/icons-vue'
const props = defineProps({
  id: {
    type: String,
    default: "barChart",
  },
  className: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "200px",
    required: true,
  },
  height: {
    type: String,
    default: "200px",
    required: true,
  },sdata: {
        type: Object,
    },
});

const chart = ref<any>("");
onMounted(() => {
  // 图表初始化
  chart.value = markRaw(
    echarts.init(document.getElementById(props.id) as HTMLDivElement)
  );
    const options = {
        grid: {
            left: "2%",
            right: "2%",
            bottom: "2%",
            containLabel: true,
        },
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "cross",
                crossStyle: {
                    color: "#999",
                },
            },
        },
        legend: {
            x: "center",
            y: "top",
            data: ["订单金额", "订单数"],
            textStyle: {
                color: "#999",
            },
        },
        xAxis: [
            {
                type: "category",
                data: props?.sdata[0]?.date,
                axisPointer: {
                    type: "shadow",
                },
            },
        ],
        yAxis: [
            {
                type: "value",
                min: 0,
                max: 56000,
                interval: 4000,
                axisLabel: {
                    formatter: "{value} $",
                },
            },
            {
                type: "value",
                min: 0,
                max: 280,
                interval: 20,
                axisLabel: {
                    formatter: "{value}",
                },
            },
        ],
        series: [
            {
                name: "OA",
                type: "bar",
                yAxisIndex: 0,
                data: [60, 65, 70, 75],
                itemStyle: {
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "#0582a9" },
                    { offset: 0.5, color: "#1badc7" },
                    { offset: 1, color: "#6499ea" },
                  ]),
                },...props?.sdata[0]||{}
            },
            {
                name: "ON",
                type: "line",
                yAxisIndex: 1,
                data: [70, 75, 80, 85],
                itemStyle: {
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "#81EC8F" },
                    { offset: 0.5, color: "#4DCB67" },
                    { offset: 1, color: "#59C293" },
                  ]),
                },...props?.sdata[1]||{}
            },
        ],
    };
  chart.value.setOption(options);

  // 大小自适应
  window.addEventListener("resize", () => {
    chart.value.resize();
  });
});
const downloadEchart = () => {
  // 获取画布图表地址信息
  const img = new Image();
  img.src = chart.value.getDataURL({
    type: "png",
    pixelRatio: 1,
    backgroundColor: "#fff",
  });
  // 当图片加载完成后，生成 URL 并下载
  img.onload = () => {
    const canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    const ctx = canvas.getContext("2d");
    if (ctx) {
      ctx.drawImage(img, 0, 0, img.width, img.height);
      const link = document.createElement("a");
      link.download = `业绩柱状图.png`;
      link.href = canvas.toDataURL("image/png", 0.9);
      document.body.appendChild(link);
      link.click();
      link.remove();
    }
  };
};
</script>
<style lang="scss" scoped>
.title {
  display: flex;
  justify-content: space-between;

  .download {
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}
</style>
